package com.zwy.space.controller.admin;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwy.space.domain.Resource;
import com.zwy.space.queryVo.ResourceTypeVo;
import com.zwy.space.queryVo.admin.ResourceVo;
import com.zwy.space.queryVo.admin.SearchResourceVo;
import com.zwy.space.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin/resource")
public class AdminResourceController {
    @Autowired
    private ResourceService resourceService;

    @RequestMapping
    public String findAllResource(Model model, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<ResourceVo> resourceVoList=resourceService.findResource();

        List<ResourceTypeVo>resourceTypeVoList=resourceService.findAllTypes();

        PageInfo<ResourceVo> pageInfo = new PageInfo<>(resourceVoList);
        model.addAttribute("pageInfoR",pageInfo);
        model.addAttribute("rtypes",resourceTypeVoList);
        return "admin/resources";
    }


    //    搜索博客
    @PostMapping("/search")
    public String search(SearchResourceVo searchResource, Model model,
                         @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        List<ResourceVo> resourcesBySerach = resourceService.findResourcesBySerach(searchResource);
        PageHelper.startPage(pageNum, 10);
        PageInfo<ResourceVo> pageInfo = new PageInfo<>(resourcesBySerach);
        model.addAttribute("pageInfoR", pageInfo);
        return "admin/resources :: resourceList";
    }

    @RequestMapping("/input")
    public String InputResource(Model model){
        model.addAttribute("rtypes",resourceService.findAllTypes());
        model.addAttribute("Aresource", new Resource());
        return "admin/resource-input";
    }



    //    博客新增
    @PostMapping("/add")
    public String post(@Valid  Resource resource, RedirectAttributes attributes, HttpServletRequest request){

        String name = request.getParameter("title");
        int typeId = Integer.parseInt(request.getParameter("type"));
        String address = request.getParameter("address");
        String description = request.getParameter("description");

        resource.setType_id(typeId);
        resource.setName(name);
        resource.setAddress(address);
        resource.setShowable(true);
        resource.setDescripe(description);

        int b = resourceService.saveResource(resource);

        if(b == 0){
            attributes.addFlashAttribute("message", "新增失败");
        }else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/resource";
    }


    @RequestMapping("/{resourceId}/input")
    public String editResource(@PathVariable int resourceId,Model model){
        Resource resourceById = resourceService.editResourceById(resourceId);
        List<ResourceTypeVo> allType = resourceService.findAllTypes();
        model.addAttribute("Aresource", resourceById);
        model.addAttribute("rtypes", allType);
        return "admin/resource-edit";
    }

    //    编辑修改文章
    @PostMapping("/{id}")
    public String editPost(@Valid ResourceVo resource, RedirectAttributes attributes,HttpServletRequest request) {

        String name = request.getParameter("title");
        int typeId = Integer.parseInt(request.getParameter("type"));
        String address = request.getParameter("address");
        String description = request.getParameter("description");
        resource.setType_id(typeId);
        resource.setName(name);
        resource.setAddress(address);
        resource.setShowable(true);
        resource.setDescripe(description);

        int b = resourceService.updateResource(resource);
        if(b ==0){
            attributes.addFlashAttribute("message", "修改失败");
        }else {
            attributes.addFlashAttribute("message", "修改成功");
        }
        return "redirect:/admin/resource";
    }

    //    删除文章
    @GetMapping("/{id}/delete")
    public String delete(@PathVariable int id, RedirectAttributes attributes) {
        resourceService.deleteResource(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/resource";
    }


}
